package NursE;

import lejos.nxt.NXTRegulatedMotor;
import lejos.nxt.SensorPort;
import lejos.nxt.UltrasonicSensor;

public class NEHead {

	/**
	 * ----------------------------------------------------------------
	 * 								Attributes
	 * ----------------------------------------------------------------
	 */
	
	protected NXTRegulatedMotor motor;
	protected UltrasonicSensor eyes;
	/**
	 * ----------------------------------------------------------------
	 * 								Static constructor
	 * ----------------------------------------------------------------
	 */
	
	public static NEHead newHeadWithMotor(NXTRegulatedMotor motor,SensorPort port) {
		NEHead head = new NEHead();
		head.setMotor(motor);
		head.setUltrasonic(port);
		return head;
	}
	
	/**
	 * ----------------------------------------------------------------
	 * 								Setters
	 * ----------------------------------------------------------------
	 */
	
	public void setMotor(NXTRegulatedMotor motor) {
		this.motor.setSpeed(50);
		this.motor = motor;
		
	}
	
	public void setUltrasonic(SensorPort port) {
		this.eyes = new UltrasonicSensor(port);
		
	}
	
	/**
	 * ----------------------------------------------------------------
	 * 								Action
	 * ----------------------------------------------------------------
	 */
		
	public boolean lookToTheRight()
	{
		this.motor.rotate(90);
		if(detection(20)==true)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	public void lookToTheFront(boolean headAtLeft)
	{
		if(headAtLeft==true){
			this.motor.rotate(90);
		}
		else{
			this.motor.rotate(-90);
		}
	}
	
	public boolean lookToTheLeft()
	{
		this.motor.rotate(-90);
		if(detection(20)==true)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	public boolean detection(float distance)
	{
		return (this.eyes.getDistance()<=distance);
	}
}
